<script setup lang="ts">
import KnowledgeCard from './KnowledgeCard.vue'
import { ref } from "vue";
import type { KnowledgeList, KnowledgeParams, KnowledgeType } from '@/types/consult';
import { getKnowledge } from '@/services/consult';
// 实现加载更多效果
const loading = ref(false)
const finished = ref(false)
const list = ref<KnowledgeList>([])

const props = defineProps<{
  type: KnowledgeType
}>()

const params = ref<KnowledgeParams>({
  type: props.type,
  current: 1,
  pageSize: 5
})
const onLoad = async () => {
  const res = await getKnowledge(params.value)
  // console.log(...res.data.rows)
  list.value.push(...res.data.rows)
  if (params.value.current >= res.data.pageTotal) {
    finished.value = true
  } else {
    params.value.current++
  }
  loading.value = false
  // 模拟数据
  // // console.log('加载中')
  // setTimeout(() => {
  //   const data = [1, 2, 3, 4, 5, 6, 7, 8, 9]
  //   list.value.push(...data)
  //   loading.value = false
  //   // 模拟数据加载完毕
  //   if (list.value.length > 20) {
  //     finished.value = true
  //   }
  // }, 1000)
}
</script>

<template>
  <div class="knowledge-list">
    <van-list v-model:loading="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
      <knowledge-card v-for="(item, prev) in list" :key="prev" :data="item"></knowledge-card>
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.knowledge-list {
  padding: 0 15px;
}
</style>